home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 2519 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.0 KB

  1. Path: newshost.lanl.gov!tanmoy
  2. From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
  3. Newsgroups: comp.lang.c,gnu.gcc.help
  4. Subject: Re: [Q] WEIRD macro bug only when compiling with GCC
  5. Date: 22 Jan 1996 04:29:16 GMT
  6. Organization: Los Alamos National Laboratory
  7. Message-ID: <TANMOY.96Jan21212916@qcd.lanl.gov>
  8. References: <DLKABL.KGK@info.physics.utoronto.ca>
  9. NNTP-Posting-Host: qcd.lanl.gov
  10. Mime-Version: 1.0
  11. Content-Type: text
  12. In-reply-to: olivers@helios.physics.utoronto.ca's message of Mon, 22 Jan 1996 02:45:21 GMT
  13.  
  14. --text follows this line--
  15. In article <DLKABL.KGK@info.physics.utoronto.ca>
  16. olivers@helios.physics.utoronto.ca (Oliver Schonborn) writes: 
  17. <snip>
  18.    The macro *works FINE* when I compile with our HP compiler,
  19.    cc: it prints out one hundred different numbers.  BUT when
  20.    I use gcc to compile, instead what it prints is double the
  21.    result of the first SQR on the line, ie  SQR( i*dx, f) * 2.
  22.    Why isn't (a=b, b*b)+(a=c, c*c) equal to b*b+c*c for GCC?
  23.  
  24. Because it invokes undefined behaviour: which means that you are doing
  25. something which the compiler need not anticipate ... and so anything
  26. could happen. There is a rule in C which says that the same object
  27. cannot be modified more than once without intervening sequence
  28. points. In your code fragment `a' is modified twice without
  29. intervening sequence point.
  30.  
  31. The issue is slightly confusing because the comma does enforce a
  32. sequence point between the previous and the next expressions:
  33. i.e. there is a sequence point between a=b and b*b; likewise between
  34. a=c and c*c. However as the + operator does not enforce _any_ order
  35. (not even the existence of an order) between its two operands, there
  36. is no sequence point _between_ a=b and a=c.
  37.  
  38. Actually your situation is still more undefined (if such a term be
  39. possible). You are talking about (a=b,a*a)+(a=c,a*a). This has the
  40. problem mentioned above, but in addition there is no sequence point
  41. between the a=c and the first a*a (likewise between the a=b and the
  42. second a*a). Thus a is both modified and independently (i.e. not as
  43. part of a process to ascertain the modified value or the value of the
  44. expression doing the modification) accessed without an intervening
  45. sequence point: this too is undefined by the same rule.
  46.  
  47.    Although I've decided to implememt the macro by a function
  48.    instead (float sqr(float x) {return x*x;}), I'm curious to
  49.    know what is wrong with the macro I wrote.  Anyone have a
  50.    clue?
  51.  
  52. Just change the second f to a g (another float) and you get a code
  53. with defined behaviour.
  54.  
  55. Thanks for posting a real life example of this error.
  56.  
  57. Cheers
  58. Tanmoy
  59. --
  60. tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
  61. Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
  62. Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
  63. <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
  64. internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
  65. fax: 1 (505) 665 3003   voice: 1 (505) 665 4733    [ Home: 1 (505) 662 5596 ]
  66.